﻿module SnakeModule {
    export interface Point {
        x: number;
        y: number;
        
    }
    /**
     * 随机生成整数。number是范围
     * @param number
     */
    export var ran = (n: number) => Math.round(Math.random() * n);
    export var ran2 = (m: number,n: number) => Math.round(Math.random() * Math.abs(m-n))+Math.min(m,n);
    /**
     * 生成随机颜色
     */
    export var ranColor = () => `rgb(${ran(255)},${ran(255)},${ran(255)})`;

    export var distance = (p1: Point, p2: Point) => Math.sqrt((p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 2);

    export var run2Point = (p1: Point, p2: Point, length: number): Point => {
        var Z = distance(p1, p2);
        if (Z < 0.5 || length == 0) return p2;

        var x = length / Z * (p2.x - p1.x) + p1.x;
        var y = length / Z * (p2.y - p1.y) + p1.y;
        return { x, y };
    }
    export var clonePoint = (p: Point): Point => { return { x: p.x, y: p.y } };

    export var isInRect = (p: Point, p1: Point, p2: Point) => {
        return ((p.x - p1.x) * (p2.x - p.x) >= 0) && ((p.y - p1.y) * (p2.y - p.y) >= 0)
    }
}


